package com.example;

public class TestDefaultMethodInInterface {

    interface One {
        default void speak(){
            sayHello();
        }

        // Java 9 开始，接口中的默认方法可以是private的
        private void sayHello(){
            System.out.println("hello, this is one");
        }
    }

    interface Two {
        default void speak(){
            System.out.println("hello, this is two");
        }
    }


    static class Impl implements One, Two {

        // 由于One, Two 接口都有相同签名的默认方法speak，因此子类必须重写这个方法，否则编译会报错
        @Override
        public void speak() {
            One.super.speak();
        }
    }

    static class Base {
        public void speak(){
            System.out.println("hello, this is Base");
        }
    }

    // Base类和One接口中都有speak方法，子类获取到的是Base类中的speak方法
    static class Derived extends Base implements One{
    }

    public static void main(String[] args) {
        new Impl().speak();
        One one = new Derived();
        one.speak(); // hello, this is Base
    }

}
